<?php

/**
 * Description of CategoryHandler
 *
 * @author Rino7
 */
require_once dirname(__FILE__) . '/../DAL/SimpleSelect.php';

class CategoryHandler {
    /**
     * create
     * read
     * update
     * delete
     * getCategories
     * groupCategories
     * renderTree
     *  
     */

    const MULTIMEDIA_CATEGORIES_TABLE_NAME = "gen_multimedia_categoria";

    private $_groupedCategories = array();
    private $_handler;

    public function __construct() {
        $this->_handler = new SimpleSelect(self::MULTIMEDIA_CATEGORIES_TABLE_NAME);
    }

    public function getCategoriesWithChilds() {
        $this->_handler->select(array("*"));
        $this->_handler->orderBy("id_padre", "DESC");
        $categories = $this->_handler->getResultsAsObjectArray();
        $this->_groupCategoriesByParentId($categories);
        return $this->_groupedCategories["root"];
    }

    private function _getCategoriesIndexedById(array $categories) {
        $total = sizeOf($categories);
        $indexedCategories = array();

        for ($i = 0; $i < $total; ++$i) {
            $indexedCategories[$categories[$i]->id] = $categories[$i];
        }
        return $indexedCategories;
    }

    private function _groupCategoriesByParentId(array $categories) {
        $indexedCategories = $this->_getCategoriesIndexedById($categories);
        foreach ($indexedCategories as $category) {
            if (!isset($indexedCategories[$category->id_padre]) && intval($category->id_padre) > 0) {
                $indexedCategories[$category->id_padre]->childs = array();
            }
            if (intval($category->id_padre) == 0) {
                $indexedCategories["root"][] = $category;
            }
            $indexedCategories[$category->id_padre]->childs[] = $category;
            unset($indexedCategories[$category->id]);
        }
        $this->_groupedCategories = $indexedCategories;
    }

    public function renderTree() {
        $categories = $this->getCategoriesWithChilds();
        $outputTree = "";

        foreach ($categories as $category) {
            $outputTree .= $this->_renderNode($category);
        }

        return $outputTree;
    }

    private function _renderNode($category) {
        $hasChilds = false;
        $class = "file";
        if (isset($category->childs) && sizeOf($category->childs) > 0) {
            $hasChilds = true;
            $class = "folder";
            $totalChilds = sizeOf($category->childs);
        }
        $outputNode = ''
                . '<li>'
                . '	<a class="rightclick" id="' . $category->id . '" href="#">'
                . '	    <span class="' . $class . '">' . $category->nombre . '</span>'
                . '	</a>';
        if ($hasChilds) {
            $outputNode .= '<ul>';
            for ($i = 0; $i < $totalChilds; ++$i) {
                $outputNode .= $this->_renderNode($category->childs[$i]);
            }
            $outputNode .= '</ul>';
        }
        $outputNode .= '</li>';
        return $outputNode;
    }

}

?>
